package com.android.functionmanagement;
import java.util.Arrays;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import com.android.functionmanagement.service.DaemonService;
import com.android.functionmanagement.util.DeviceUtils;
import com.android.functionmanagement.util.SharedPreferencesUtils;

import android.app.Application;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;


/**
 * 描述:BaseApplication
 * <p />
 * 
 * 基础Application
 * 
 * 
 */
public class BaseApplication extends Application {
	
	private static final String TAG = "BaseApplication"; 
	
	@Override
	public void onCreate() {
		super.onCreate();
		Log.i(TAG, "onCreate...");
		ControlCenter controlCenter = ControlCenter.getInstanse();
		controlCenter.init();
		//启动业务逻辑守护进程
		Intent it = new Intent(this, DaemonService.class);
		startService(it);
		
		//JPushInterface.setDebugMode(true);
//		JPushInterface.setAlias(this, DeviceUtils.getIMEI(this),
//				new TagAliasCallback() {
//
//					@Override
//					public void gotResult(int arg0, String arg1,
//							Set<String> arg2) {
//						Log.d(TAG, "arg0:"+arg0 + "arg1:" +arg1);
//
//					}
//				});
		initJpush();
	}
	
	public void initJpush() {
		//JPushInterface.setDebugMode(true);
	    JPushInterface.init(this);
		//if(!Boolean.parseBoolean(SharedPreferencesUtils.getParam(this, "isSuccessed", false).toString())){
		mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS,DeviceUtils.getIMEI(this)));
		//}
		if (JPushInterface.isPushStopped(this)){
			JPushInterface.resumePush(this);
		}
	}
	
	private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
	    @Override
	    public void gotResult(int code, String alias, Set<String> tags) {
	        String logs ;
	        switch (code) {
	        case 0:
	        	if((alias != null) && !("".equals(alias))){
					mHandler.removeMessages(MSG_SET_ALIAS);
				}
	            logs = "Set tag and alias success";
	            Log.i(TAG, logs);
	            // 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。
	            SharedPreferencesUtils.setParam(BaseApplication.this, "isSuccessed", true);
	            break;
	        case 6002:
	            logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
	            Log.i(TAG, logs);
	            // 延迟 60 秒来调用 Handler 设置别名
            	JPushInterface.init(BaseApplication.this);
	            mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
	            break;
	        default:
	            logs = "Failed with errorCode = " + code;
	            Log.e(TAG, logs);
	        }
	        String[] tgs = new String[]{};
	        if (tags != null){
				tgs = tags.toArray(new String[]{});
			}
			Log.d(TAG, "==result code:" + code + ",alias:" + alias + ",tags:"+ Arrays.toString(tgs));
	    }
	};
	private static final int MSG_SET_ALIAS = 1001;
	private final Handler mHandler = new Handler() {
	@Override
	    public void handleMessage(android.os.Message msg) {
	        super.handleMessage(msg);
	        switch (msg.what) {
	            case MSG_SET_ALIAS:
	            	Log.d(TAG, "Set alias in handler.");
	                // 调用 JPush 接口来设置别名。
	                JPushInterface.setAliasAndTags(getApplicationContext(),
	                                                (String) msg.obj,
	                                                 null,
	                                                 mAliasCallback);
	            break;
	        default:
	        	Log.i(TAG, "Unhandled msg - " + msg.what);
	        }
	    }                                       
	};

	public void onTerminate() {
		
	};

}
